//
//  iToastImage.m
//  点击图片放大滑动切换图片
//
//  Created by apple on 16/1/13.
//  Copyright © 2016年 马红涛. All rights reserved.
//

#import "iToastImage.h"
#define CURRENT_TOAST_TAG 6984678
@implementation iToastImage

+(id)makeinit{
    iToastImage *toast = [[iToastImage alloc] init] ;
    return toast;
}


- (void)showImg:(UIImage *)img{

    UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    self.imgView.image = img;
    self.imgView.frame = CGRectMake(0, 0, 105, 105);
    self.imgView.center = window.center;
    
    NSTimer *timer1 = [NSTimer timerWithTimeInterval:1500/1000
                                              target:self selector:@selector(hideToast:)
                                            userInfo:nil repeats:NO];
    [[NSRunLoop mainRunLoop] addTimer:timer1 forMode:NSDefaultRunLoopMode];
    

    self.imgView.tag = CURRENT_TOAST_TAG;
    UIView *currentToast = [window viewWithTag:CURRENT_TOAST_TAG];
    if (currentToast != nil) {
        [currentToast removeFromSuperview];
    }
    self.imgView.alpha = 0;
    [window addSubview:self.imgView];
    [UIView beginAnimations:nil context:nil];
    self.imgView.alpha = 1;
    [UIView commitAnimations];

}

- (void) hideToast:(NSTimer*)theTimer{
    [UIView beginAnimations:nil context:NULL];
    self.imgView.alpha = 0;
    [UIView commitAnimations];
    
    NSTimer *timer2 = [NSTimer timerWithTimeInterval:500
                                              target:self selector:@selector(hideToast:)
                                            userInfo:nil repeats:NO];
    [[NSRunLoop mainRunLoop] addTimer:timer2 forMode:NSDefaultRunLoopMode];
}

- (UIImage *)image{
    if (!_image) {
        _image = [[UIImage alloc]init];
    }
    return _image;
}
-(UIImageView *)imgView{
    if (!_imgView) {
        _imgView = [[UIImageView alloc]init];
    }
    return _imgView;
}
@end
